export default class ArrayUtils {

    static deleteStr(str: string, arr: string[]) {
        let index = arr.findIndex((v) => v == str);
        arr.splice(index, 1);
    }


    /**
     * 批量删除
     * @param arrayData 数组
     * @param multipleDeleteData 需删除数组
     */
    static delete(arrayData: any[], multipleDeleteData: any[]) {
        for (let k = 0; k < multipleDeleteData.length; k++) {
            arrayData.splice(arrayData.findIndex(item => item.id === multipleDeleteData[k].id), 1);
        }
    }

    /**
     * 批量上移
     * @param arrayData 数组
     * @param multipleMoveUpData 需上移数组
     */
    static moveUp(arrayData: any[], multipleMoveUpData: any[]) {
        let arr = [];
        for (let i = 0; i < multipleMoveUpData.length; i++) {
            arr.push(arrayData.findIndex(item => item.id === multipleMoveUpData[i].id));
        }
        arr.sort((n1, n2) => n1 - n2);

        for (let k = 0; k < arr.length; k++) {
            let add: any = arrayData;
            if (arr[k] != 0) {
                arrayData[arr[k]] = arrayData.splice(arr[k] - 1, 1, add[arr[k]])[0];
            } else {
                arrayData.push(arrayData.shift());
            }
        }
    }

    /**
     * 批量下移
     * @param arrayData 数组
     * @param multipleMoveDownData 需下移数组
     */
    static moveDown(arrayData: any[], multipleMoveDownData: any[]) {
        let arr = [];
        for (let i = 0; i < multipleMoveDownData.length; i++) {
            arr.push(arrayData.findIndex(item => item.id === multipleMoveDownData[i].id));
        }
        arr.sort((n1, n2) => n2 - n1);

        for (let k = 0; k < arr.length; k++) {
            let add: any = arrayData;
            if (arr[k] != arrayData.length - 1) {
                arrayData[arr[k]] = arrayData.splice(arr[k] + 1, 1, add[arr[k]])[0];
            } else {
                arrayData.unshift(arrayData.splice(k, 1)[0]);
            }
        }
    }

    /**
     * 批量置顶
     * @param arrayData 数组
     * @param multipleToppingData 需置顶数组
     */
    static topping(arrayData: any[], multipleToppingData: any[]) {
        let arr = null;
        for (let k = multipleToppingData.length - 1; k >= 0; k--) {
            arr = arrayData.findIndex(
                item => item.id === multipleToppingData[k].id
            );
            if (-1 < arr) {
                arrayData.unshift(arrayData.splice(arr, 1)[0]);
            }
        }
    }

    /**
     * 批量置底
     * @param arrayData 数组
     * @param multipleSettingData 需置底数组
     */
    static setting(arrayData: any[], multipleSettingData: any[]) {
        let arr = null;
        for (let k = 0; k < multipleSettingData.length; k++) {
            arr = arrayData.findIndex(item => item.id === multipleSettingData[k].id);

            let add: any = arrayData;
            if (arr != arrayData.length - 1) {
                arrayData.push(add[arr]);
                arrayData.splice(arr, 1);
            }
        }
    }

    /**
     * 分割数组
     * @param arr
     * @param limit
     */
    static splitArray(arr: any[], limit: number = 1): any[] {
        if (1 == limit || null == limit) {
            return arr;
        }
        let array = [], temp = [];
        for (let i = 0; i < arr.length; i += limit) {
            temp = [];
            for (let j = i; j < i + limit; j++) {
                if (null != arr[j]) {
                    temp.push(arr[j]);
                }
            }
            array.push(temp);
        }
        return array;
    }

    /**
     * 过滤数组
     * @param arr
     * @param filterFunc
     */
    static filter(arr: any[], filterFunc: (item: any, index: number) => boolean): any[] {
        let filterArr: any[] = [],
            item: any = null;
        for (let i = 0; i < arr.length; i++) {
            item = arr[i];
            filterFunc(item, i) && (filterArr.push(item));
        }
        return filterArr;
    }

    /**
     * 过滤数组(获取符合条件的第一个元素)
     * @param arr
     * @param filterFunc
     */
    static filterFirst(arr: any[], filterFunc: (item: any, index: number) => boolean): any {
        let item: any = null;
        for (let i = 0; i < arr.length; i++) {
            item = arr[i];
            if (filterFunc(item, i)) {
                return item;
            }
        }
        return null;
    }

    /**
     * 删除
     * @param arr
     * @param start
     * @param deleteCount
     */
    static remove(arr: any[], start: number, deleteCount: number = 1) {
        arr.splice(start, deleteCount);
    }

    /**
     * 安全删除
     * @param arr
     * @param removeFunc
     */
    static safeRemove(arr: any[], removeFunc: (item: any, index: number) => boolean) {
        if (null != arr) {
            let removeArr: any[] = [];
            for (let i = 0; i < arr.length; i++) {
                removeFunc(arr[i], i) && (removeArr.push(i));
            }
            for (let i = removeArr.length - 1; i >= 0; i--) {
                this.remove(arr, removeArr[i]);
            }
        } else {
            console.error("[ArrayUtils] array == null");
        }
    }

    /**
     * 合并
     * @param arr
     * @param mergeArr
     */
    static merge(arr: any[], mergeArr: any[]) {
        if (null != mergeArr) {
            for (let i = 0; i < mergeArr.length; i++) {
                arr.push(mergeArr[i]);
            }
        }
    }

    static randomPick(arr: any[]) {
        // 取值范围[0,length-1]
        let randomIndex = Math.floor(Math.random() * arr.length);
        return arr[randomIndex];
    }

    static randomIndexWithWeight(weightArr: number[]) {
        let randomIndex = 0;
        let weightItemArr: { index: number, weight: number }[] = [];
        let totalWeight = 0;
        for (let i = 0; i < weightArr.length; i++) {
            let weight = weightArr[i];
            weightItemArr.push({ index: i, weight: weight });
            totalWeight += weight;
        }

        // 生成一个随机数，范围在[0, totalWeight)之间
        const randomValue = Math.random() * totalWeight;
        // 累加权重，直到累计值超过随机数，返回对应索引的元素
        let cumulativeWeight = 0;
        for (let i = 0; i < weightItemArr.length; i++) {
            cumulativeWeight += weightItemArr[i].weight;
            if (cumulativeWeight >= randomValue) {
                randomIndex = weightItemArr[i].index;
                break;
            }
        }
        return randomIndex;
    }
}